package main

import (
	"net/http"

	"github.com/anaskhan96/go-password-encoder"
	"github.com/gin-gonic/gin"
)
//开发一个http层
func main() {
	//用来存放加密后的md5值
	var passwd = [2]string{}
	router := gin.Default()
	//开放get路由 http://127.0.0.1:8021/enpwd?enpwd=???
	router.GET("/enpwd", func(c *gin.Context) {
		//接受密码
		enpwd := c.Query("enpwd")
		//加密
		salt, encodedPwd := password.Encode(enpwd, nil)
		passwd[0] = salt
		passwd[1] = encodedPwd
		if enpwd == "" {
			c.JSON(http.StatusOK, gin.H{
				"msg": "请在get里填充enpwd",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"msg": "加密成功,给你md5值你也解不出来",
			"code":encodedPwd,
		})
	})
	//开放get路由 http://127.0.0.1:8021/unpwd?unpwd=???
	router.GET("/unpwd", func(c *gin.Context) {
		//接受密码
		unpwd := c.Query("unpwd")
		//解密
		check := password.Verify(unpwd, passwd[0], passwd[1], nil)
		if !check {
			c.JSON(http.StatusOK, gin.H{
				"msg": "密码错误",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"msg": "密码正确",
		})
	})
	_ = router.Run(":8021")
}
